package risk.client.logic;

import java.awt.Color;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

import risk.client.Risk;

/**
 * @author: Mike
 * @deprecated
 */
public class CPU extends Player implements java.io.Serializable {    
    /**
     * Create a new CPU user
     * 
     * @author: Mike
     * @param risk
     */
    public CPU(int id, String name, Color color, Risk risk) {
        super(id, name, color, risk);
    }        
    
    @Override
    public synchronized Node getSelectedNode() { 
        Node node = null, node2 = null, nodeOwn = null, nodeOpp = null; 
        
        //if the computer have used fortify it should pass
        if (usedFortify() == true) {
            endTurn(true);
        }
      
        // If node equals null, it means that it should select the first node
        if (risk.board.getSelectedNode() == null) {
            // Get a list with nodes owned by the player
            List<Node> list = risk.board.getGrid().getOwnerNodes(this);
          
            if(!isReinforced()) {
                // Initial turn move, reinforce random node
                node = list.get(randomChoice(1, list.size()) - 1); //gets the node with the random index
            } else {
                /**
                 * We have reinforced, so no go on and select the first node
                 * with an army with more than one unit
                 */
                Iterator i = list.iterator();
                  
                while(i.hasNext()) {
                    node2 = (Node)i.next();
                    // If computers node and the node has more than one unit
                    if(node2.getArmy().getOwner().getId() == getId() && node2.getArmy().getUnits() > 1) {
                        // if node is null or there are more units than the previous
                        if(node == null || node2.getArmy().getUnits() > node.getArmy().getUnits()) {
                            node = node2;
                        }
                    }
                    
                    /*
                     * If we have looped through all nodes and the last node
                     * only has one army, we can't select another node
                     * so just end the turn.
                     */
                    if(node == null) {
                        endTurn(true);
                    }
                }
            }
        } else { // Get a second node
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                System.out.println(e.toString());
            }
            
            if(oneOnEach() == true) {
                endTurn(true);
            } else {
                //sets node to the first choosen one
                node = risk.board.getSelectedNode();
                
                //List with nodes connected to the first choosen one
                List<Node> list = node.getConnections();
                Iterator i = list.iterator();

                while(i.hasNext()) {
                    node2 = (Node)i.next();
                    
                    /**
                     * If connected node is owned by computer we want to
                     * check if that node has less units than the previous selected one
                     * (or if we havn't selected a previous one)
                     */
                    if(node2.getArmy().getOwner().getId() == getId()) {
                        if(nodeOwn == null || nodeOwn.getArmy().getUnits() < node2.getArmy().getUnits()) {
                            nodeOwn = node2;
                        }
                    } else {
                        /**
                         * Same if the connected node is an opponent node
                         */
                        if(nodeOpp == null || nodeOpp.getArmy().getUnits() < node2.getArmy().getUnits()) {
                            nodeOpp = node2;
                        }
                    }
                } // End while i.hasNext()
              
                /**
                 * Finally, attack has priority over fortify
                 */
                node = (nodeOpp != null) ? nodeOpp : nodeOwn;
           }
        }
         
        return node;
    }
        
    /**
     * Check if it's only one unit in all computers nodes
     * 
     * @author: Mike
     */
    private boolean oneOnEach(){       
        //a list with nodes that the player ownes
        List<Node> list = risk.board.getGrid().getOwnerNodes(this);
        
        for(int i = 0; i < list.size(); i++) {
            if(list.get(i).getArmy().getUnits() > 1) {
                return false;
            }
        }
           
        return true;
    }  
    
    /**
     * Method to get a random number
     * 
     * @author: Mike
     */
    private int randomChoice(int min, int max){
        Random random = new Random(System.nanoTime());
        return random.nextInt(max-min) + min;
    } 
    
    /**
     * Reinforce node
     *
     * @author: Johan
     * @param node
     */
    public void reinforce(Node node) {
        node.getArmy().increaseUnits(1);
        setReinforced(true);
    }     
}